package cn.newtouch.product.controller;

import cn.newtouch.basic.exception.BusinessException;
import cn.newtouch.basic.util.JsonResult;
import cn.newtouch.basic.util.PageList;
import cn.newtouch.product.domain.Product;
import cn.newtouch.product.query.ProductQuery;
import cn.newtouch.product.service.IProductService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    public IProductService productService;

    /**
     * 接口：添加或修改
     * @param product  传递的实体
     * @return JsonResult 响应给前端
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Product product){
        try {
            if( product.getId()!=null)
                productService.update(product);
            else
                productService.save(product);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return JsonResult 响应给前端
    */
    @DeleteMapping("/{id}")
    public JsonResult deleteById(@PathVariable("id") Long id){
        try {
            productService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return JsonResult 响应给前端
    */
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
                productService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Product findOne(@PathVariable("id")Long id) {

        return productService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Product> findAll(){
        return productService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Product> queryPage(@RequestBody ProductQuery query) {

        return productService.queryPage(query);
    }



    /**
     * 上架
     * @return
     */
    @PostMapping("/onsale")
    @ApiOperation(value = "上架" )
    public JsonResult onSale(@RequestBody List<Long> ids){
        try {
            productService.onSale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 下架
     * @return
     */
    @PostMapping("/offsale")
    @ApiOperation(value = "下架" )
    public JsonResult offSale(@RequestBody List<Long> ids){
        try {
            productService.offSale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }


}
